package com.pozu.new_retail.controllers;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.CardTags;
import com.pozu.new_retail.service.CardTagsService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 名片 标签 Coltroller
 *
 * @author shenxueyuan
 * @Date 2019/3/15
 */
@Controller
public class CardTagsController {

    @Autowired
    private CardTagsService cardTagsSergice;

    @ResponseBody
    @RequestMapping(value = "/api/card/tag/list", method = RequestMethod.GET)
    public Object queryTagsByMap(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//哪个小程序

        String staffId = request.getParameter("staffId");//工作人员id

        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("appId", appId);
        hashMap.put("staffId", staffId);

        JsonResult jr = new JsonResult();

        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        jr.respData = cardTagsSergice.queryTagsByMap(hashMap);
        return jr;
    }

    /**
     * 新增一个 标签
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/tag/save", method = RequestMethod.POST)
    public Object addCardTags(HttpServletRequest request) throws Exception {

        int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序

        CardTags cardTags = Tools.getRequestObject(request, CardTags.class);
        cardTags.setAppId(appId);
        cardTags.setCreate_time(System.currentTimeMillis());

        JsonResult jr = new JsonResult();
        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        cardTagsSergice.insertCardTags(cardTags);

        return jr;
    }

    /**
     * 更新标签 同意人数
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/api/card/tag/update", method = RequestMethod.PUT)
    public Object updateCardTagsCnt(HttpServletRequest request) throws Exception {

        CardTags cardTags = Tools.getRequestObject(request, CardTags.class);

        JsonResult jr = new JsonResult();
        jr.respCode = CodeConstants.SUCCESS_CODE;
        jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);

        cardTagsSergice.updateCardTagsCnt(cardTags);

        return jr;
    }

}
